package com.piaohailin.feature.ladp;

import javax.naming.AuthenticationException;
import javax.naming.Context;
import javax.naming.NamingEnumeration;
import javax.naming.NamingException;
import javax.naming.directory.Attribute;
import javax.naming.directory.Attributes;
import javax.naming.directory.BasicAttribute;
import javax.naming.directory.BasicAttributes;
import javax.naming.directory.DirContext;
import javax.naming.directory.SearchControls;
import javax.naming.directory.SearchResult;
import javax.naming.ldap.Control;
import javax.naming.ldap.InitialLdapContext;
import javax.naming.ldap.LdapContext;
import java.util.Hashtable;

/**
 * @Author: piaohailin* @Date: 2019-11-01 18:05
 */
class LDAP {
    private final String URL = "ldap://172.16.209.10:389/";
    private final String BASEDN = "dc=12dong,dc=cn";  // 根据自己情况进行修改
    private final String FACTORY = "com.sun.jndi.ldap.LdapCtxFactory";
    private LdapContext ctx = null;
    private final Control[] connCtls = null;


    private LDAP() {
        Hashtable<String, String> env = new Hashtable<String, String>();
        env.put(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
        env.put(Context.PROVIDER_URL, URL + BASEDN);
        env.put(Context.SECURITY_AUTHENTICATION, "simple");

        String root = "cn=admin,dc=12dong,dc=cn";  //根据自己情况修改
        env.put(Context.SECURITY_PRINCIPAL, root);   // 管理员
        env.put(Context.SECURITY_CREDENTIALS, "ruopu1013");  // 管理员密码

        try {
            ctx = new InitialLdapContext(env, connCtls);
            System.out.println("连接成功");

        } catch (javax.naming.AuthenticationException e) {
            System.out.println("连接失败：");
            e.printStackTrace();
        } catch (Exception e) {
            System.out.println("连接出错：");
            e.printStackTrace();
        }

    }


    private void closeContext() {
        if (ctx != null) {
            try {
                ctx.close();
            }
            catch (NamingException e) {
                e.printStackTrace();
            }
        }
    }

    void searchRole() throws NamingException {

        System.out.println("Searching...");
        SearchControls searchCtls = new SearchControls();

        // Specify the search scope
        searchCtls.setSearchScope(SearchControls.SUBTREE_SCOPE);
        // specify the LDAP search filter
        String searchFilter = "objectClass=*";

        // Specify the Base for the search
        String searchBase = "cn=erp-admim,ou=role";
        // Specify the attributes to return
//        String[] returnedAtts = { "cn" };
//        searchCtls.setReturningAttributes(returnedAtts);

        // Search for objects using the filter

        NamingEnumeration<SearchResult> entries = ctx.search(searchBase, searchFilter, searchCtls);

        System.out.println("entries : " + entries);
        while (entries.hasMoreElements()) {
            SearchResult entry = entries.next();
            System.out.println(">>>" + entry.getName());

            // Print out the groups
            Attributes attrs = entry.getAttributes();

            System.out.println(attrs.get("memberuid"));
            System.out.println(attrs.get("labeleduri"));
            if (attrs != null) {
                for (NamingEnumeration<? extends Attribute> names = attrs
                        .getAll(); names.hasMore();) {
                    Attribute attr = names.next();
                    System.out.println("AttributeID: " + attr.getID());
                    for (NamingEnumeration<?> e = attr.getAll(); e.hasMore();) {
                        System.out.println("Attributes:" + e.next());
                    }
                }
            }
        }
        System.out.println("Search complete.");
    }


    public static void main(String[] args) {
        LDAP ldap = new LDAP();
        try {
            ldap.searchRole();
        } catch (NamingException e) {
            e.printStackTrace();
        }
//        ldap.getUserDN("yorker");

//        if(ldap.authenricate("yorker", "secret") == true){
//
//            System.out.println( "该用户认证成功" );
//
//        }
//        ldap.addUser("czj333","123456");
        ldap.closeContext();
    }

}
